package com.wsw.aitools.doubao;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wsw.aitools.utils.ImageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.awt.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class DoubaoApi {
    
    public static final String apiKey = "158f035c-1d3e-4b0d-bf1c-63edef2fe1fd";
    public static final String modelName = "ep-20241224182005-8vh2m";
    // 豆包接口地址    
    public static final String url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
    
    public String askDoubao(String prompt, String fileUrl){
        
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(apiKey);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("model", modelName);

        Map<String, Object> textContent = new HashMap<>();
        textContent.put("type", "text");
        textContent.put("text", prompt);
        

        Map<String, Object> imageUrl1 = new HashMap<>();
        imageUrl1.put("type", "image_url");
        imageUrl1.put("image_url", new HashMap<String, String>() {{

            put("url", ImageUtils.encodeImage(fileUrl));
        }});

//        Map<String, Object> imageUrl2 = new HashMap<>();
//        imageUrl2.put("type", "image_url");
//        imageUrl2.put("image_url", new HashMap<String, String>() {{
//            put("url", "https://portal.volccdn.com/obj/volcfe/cloud-universal-doc/upload_a81e3cdd3e30617ecd524a132fdb2736.png");
//        }});

        Map<String, Object> message = new HashMap<>();
        message.put("role", "user");
        message.put("content", new Object[]{textContent, imageUrl1});

        requestBody.put("messages", new Object[]{message});

        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);

        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

        String body = response.getBody();
        System.out.println("Response: " + response.getBody());

        // 转fastjson
        JSONObject jsonObject = JSON.parseObject(body);
        JSONArray choices = jsonObject.getJSONArray("choices");

        JSONObject choice1  = (JSONObject) choices.get(0);
        JSONObject messageJson = choice1.getJSONObject("message");
        String content = messageJson.getString("content");
        System.out.println("content: " + content);
        
        return content;
    }
    
    
}
